#include "stdafx.h"
#include "snclientplugin_i.h"

//
// Flow macros
// 

static
void
WINAPI
_OutputDbgStr(
__in  LPSTR szFile,
__in  DWORD dwLine,
__in  LPSTR szMsg,
__in  DWORD dwStatus)
{
    CHAR rgsz[256];
    LPSTR szTag = "INFO";

    if (0 != dwStatus)
        szTag = "ERROR";

    StringCbPrintfA(
        rgsz,
        sizeof(rgsz),
        "%s: %s - 0x%x, file %s, line %d\n",
        szTag,
        szMsg,
        dwStatus,
        szFile,
        dwLine);
    printf(rgsz);
}

#define CHECK_COM(_X) {                                             \
    if (FALSE == SUCCEEDED(hr = _X)) {                              \
        _OutputDbgStr(__FILE__, __LINE__, #_X, (DWORD) hr);         \
        goto Cleanup;                                               \
    }                                                               \
}

#define CHECK_DWORD(_X) {                                           \
    if (0 != (status = _X)) {                                       \
        _OutputDbgStr(__FILE__, __LINE__, #_X, status);             \
        goto Cleanup;                                               \
    }                                                               \
}

DWORD
WINAPI
_EnumProducts()
{
    DWORD status = 0;
    DWORD dwIndex = 0;
    WCHAR rgwszProductCode[MAX_PATH];
    WCHAR rgwszLocalPackage[MAX_PATH];
    DWORD cchLocalPackage = 0;

    while (0 == (status = MsiEnumProductsExW(
        NULL,
        NULL,
        MSIINSTALLCONTEXT_MACHINE,
        dwIndex,
        rgwszProductCode,
        NULL,
        NULL,
        NULL)))
    {
        cchLocalPackage = 
            sizeof(rgwszLocalPackage) / sizeof(rgwszLocalPackage[0]);
        CHECK_DWORD(MsiGetProductInfoExW(
            rgwszProductCode,
            NULL,
            MSIINSTALLCONTEXT_MACHINE,
            INSTALLPROPERTY_LOCALPACKAGE,
            rgwszLocalPackage,
            &cchLocalPackage));
        dwIndex++;
        printf("%S\n", rgwszLocalPackage);
    }

    if (ERROR_NO_MORE_ITEMS == status)
        status = 0;

Cleanup:
    return status;
}

//
// Test entry point
//
int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr = S_OK;
    CComPtr<ISnCliPlugin> pSnCliPlugin = NULL;
    ULONG ulIsSecure = 0;
    LPWSTR wszMessage = NULL;

    UNREFERENCED_PARAMETER(argv);

    if (1 != argc)
    {
        printf("Usage: SnPluginTstNat.exe \n");
        goto Cleanup;
    }

    CHECK_COM(CoInitialize(NULL));

    CHECK_COM(CoCreateInstance(
        CLSID_SnCliPlugin,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_PPV_ARGS(&pSnCliPlugin)));

    //
    // Test health status interface
    //

    CHECK_COM(pSnCliPlugin->NotifySecurityStatus(ulIsSecure));

    CHECK_COM(pSnCliPlugin->GetSecurityStatus(&ulIsSecure, &wszMessage));

    printf(
        "Health status: %d %S\n",
        ulIsSecure,
        wszMessage);

    //
    // Test Windows Installer interface
    //

    CHECK_COM(HRESULT_FROM_WIN32(_EnumProducts()));

Cleanup:
    if (NULL != wszMessage)
        CoTaskMemFree(wszMessage);
    return hr;
}

